package a66weding.com.jiehuntong.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;


import com.andview.refreshview.XRefreshView;

import java.util.HashMap;
import java.util.List;

import a66weding.com.jiehuntong.R;

import a66weding.com.jiehuntong.adapter.MainHelpAdapter;
import a66weding.com.jiehuntong.beans.CommonResultBean;
import a66weding.com.jiehuntong.beans.ErrorBean;
import a66weding.com.jiehuntong.beans.FriendsListBean;
import a66weding.com.jiehuntong.beans.HttpRequestBean;

import a66weding.com.jiehuntong.beans.MarriageProposalBean;
import a66weding.com.jiehuntong.beans.SuccessBean;
import a66weding.com.jiehuntong.http.HttpHandleUtil;
import a66weding.com.jiehuntong.http.OnNetResultListener;
import a66weding.com.jiehuntong.http.RequestCode;
import a66weding.com.jiehuntong.http.UrlDealUtil;

/**
 * Created by admin on 2016/9/26.
 */
public class MainHelpActivity1  extends Activity implements OnNetResultListener,AdapterView.OnItemClickListener{

    private ListView mainhelp_lv;
    private MainHelpAdapter adapter;
    private MarriageProposalBean.DataBean dataBean;
    private TextView findhelp_title;
    private TextView title_tv;
    public  static  String MAIN_HELP_NAME = "main_help_name";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mainhelp);
        dataBean = (MarriageProposalBean.DataBean) getIntent().getSerializableExtra(JobDetail.TASK_FLAG);
        initView();
        initRefreshView();
        getHelpFriends();
    }

    private void initView(){
        mainhelp_lv = (ListView) findViewById(R.id.mainhelp_lv);
        adapter = new MainHelpAdapter(this,null);
        mainhelp_lv.setAdapter(adapter);
        mainhelp_lv.setOnItemClickListener(this);
        findhelp_title = (TextView) findViewById(R.id.title_tv);
        findhelp_title.setText("选择帮友");
        title_tv = (TextView) findViewById(R.id.back_iamge);
        title_tv.setText(""+dataBean.getBookTitle());
    }

    private XRefreshView xRefreshView;
    private boolean isDownRefresh = true;
    private void initRefreshView(){
        xRefreshView = (XRefreshView)findViewById(R.id.main_help_XR);
        //是否允许下拉刷新
        xRefreshView.setPullRefreshEnable(true);
        // 设置是否可以上拉加载
     //   xRefreshView.setPullLoadEnable(true);
        // 设置刷新完成以后，headerview固定的时间
        xRefreshView.setPinnedTime(1000);
        // 静默加载模式不能设置footerview
        // 设置支持自动刷新
        xRefreshView.setAutoLoadMore(true);

        xRefreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {

            @Override
            public void onRefresh() {
                getHelpFriends();
            }

            @Override
            public void onLoadMore(boolean isSlience) {

            }
        });
    }


    private FriendsListBean friendsListBean;
    private List<FriendsListBean.DataBean> list_bean;
    private CommonResultBean commonResultBean;
    /**
     * 获取好友列表;
     */
    private void getHelpFriends(){
        String useUrl = UrlDealUtil.getRealUrl(RequestCode.SELECT_MEMBERID);
        HttpRequestBean requestBean = new HttpRequestBean(RequestCode.POSTEQUEST, useUrl, this, FriendsListBean.class, RequestCode.SELECT_MEMBERID);
        HttpHandleUtil.executeCommonPost(this, requestBean);
    }

    @Override
    public void onSucess(SuccessBean sucessbean) {
        if (isDownRefresh){
            xRefreshView.stopRefresh();
        }else{
            xRefreshView.stopLoadMore();
        }
        Object data = sucessbean.getData();
        if (data instanceof  FriendsListBean){
            friendsListBean = (FriendsListBean) data;
            switch (friendsListBean.getResCode()){
                case "0000":
                    list_bean = friendsListBean.getData();
                    if (isDownRefresh){
                        adapter.clear();
                    }
                    adapter.append( getFilter(list_bean));
                    break;
            }
        }


        if (data instanceof CommonResultBean){
            commonResultBean = (CommonResultBean) data;
            switch (commonResultBean.getResCode()){
                case "0000":
                    Intent intent = new Intent();
                    intent.putExtra(MAIN_HELP_NAME,dataBean);
                    setResult(7,intent);
                    finish();
                    break;
            }
        }
    }
    @Override
    public void onError(ErrorBean errorBean) {

    }

    private List<FriendsListBean.DataBean> getFilter(List<FriendsListBean.DataBean> list){
        if (list.size()>1){
            List<FriendsListBean.DataBean> friends =list;
            for (int i  = 0 ;i<list.size();i++){
                for (int j  = list.size()-1 ;j>i;j--){
                    if (list.get(i).getMemberRID()==friends.get(j).getMemberRID()){
                        friends.remove(j);
                    }
                }
            }
            return friends;
        }
        return list;
    }
    private String name;
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        view.findViewById(R.id.diy_progress_flag).setVisibility(View.VISIBLE);
        int ids= list_bean.get(position).getMemberRID();
        name = list_bean.get(position).getName();
        dataBean.setNvc_Name(list_bean.get(position).getName());
        personnelChanges(ids);
    }

    //接受好友帮助的状态;
    private void personnelChanges(int id){
        String userUrl = UrlDealUtil.getRealUrl(RequestCode.UPDATE_ASSISTANT);
        HashMap<String,String> paramsMap=new HashMap<>();
        paramsMap.put("BookTitle",dataBean.getBookTitle());
        paramsMap.put("AssistantID",""+id);
        //
        HttpRequestBean httpRequestBean = new
                HttpRequestBean
                (RequestCode.POSTEQUEST,userUrl,this,CommonResultBean.class,RequestCode.UPDATE_ASSISTANT,paramsMap);
        HttpHandleUtil.executeCommonPost(this,httpRequestBean);
    }

    public void back(View view){
        finish();
    }
}
